package com.group1.ws;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Session;

import com.group1.model.AnswerItem;
import com.group1.model.Lecturer;
import com.group1.model.Question;
import com.group1.model.Topic;
import com.group1.util.SessionFactoryUtil;

@Path("/answer")
public class AnswerWS {


	@GET
	@Path("/getAll")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAllAnswer()
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<AnswerItem> list =  AnswerItem.getAllAnswer(session);
		String result;
		result = "{ANSWER:[";
		boolean first = true;
		for (AnswerItem a : list) {
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"answerID\"" + ":\"" + a.getAnsiId() + "\",";
			result += "\"questionID\"" + ":\"" + a.getQuestion().getQuestionId() + "\",";
			result += "\"answerDesc\"" + ":\"" + a.getAnsiDesc() + "\",";
			result += "\"answerValue\"" + ":\"" + a.getAnsiValue() + "\",";
			result += "\"solution\"" + ":\"" + a.getAnsiIsSolution() + "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByID/{answerID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAnswerByID(@PathParam("answerID") String answerID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		AnswerItem a = AnswerItem.getAnwserByID(Integer.valueOf(answerID), session);
		String result;
		
		result = "{ANSWER:";
		result += "{";
		result += "\"answerID\"" + ":\"" + a.getAnsiId() + "\",";
		result += "\"questionID\"" + ":\"" + a.getQuestion().getQuestionId() + "\",";
		result += "\"answerDesc\"" + ":\"" + a.getAnsiDesc() + "\",";
		result += "\"answerValue\"" + ":\"" + a.getAnsiValue() + "\",";
		result += "\"solution\"" + ":\"" + a.getAnsiIsSolution() + "\"";
		result += "}}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByQuestion/{questionID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAnswerByQuestion(@PathParam("questionID") String questionID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<AnswerItem> list = AnswerItem.getAnswerByQuestion(Integer.valueOf(questionID), session);
		String result;
		result = "{ANSWER:[";
		boolean first = true;
		for (AnswerItem a : list) {
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"answerID\"" + ":\"" + a.getAnsiId() + "\",";
			result += "\"questionID\"" + ":\"" + a.getQuestion().getQuestionId() + "\",";
			result += "\"answerDesc\"" + ":\"" + a.getAnsiDesc() + "\",";
			result += "\"answerValue\"" + ":\"" + a.getAnsiValue() + "\",";
			result += "\"solution\"" + ":\"" + a.getAnsiIsSolution() + "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/create/{questionID}/{answerDesc}/{answerValue}/{solution}")
	@Produces(MediaType.TEXT_PLAIN)
	public String createAnswer(@PathParam("questionID") String questionID, @PathParam("answerDesc") String answerDesc, 
			@PathParam("answerValue") String answerValue, @PathParam("solution") String solution)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	AnswerItem a = new AnswerItem();
			 	a.setAnsiDesc(answerDesc);
			 	a.setQuestion(Question.getQuestionByID(Integer.valueOf(questionID), session));
			 	a.setAnsiValue(answerValue);
			 	a.setAnsiIsSolution(Boolean.valueOf(solution));
			   	session.save(a);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/update/{answerID}/{questionID}/{answerDesc}/{answerValue}/{solution}")
	@Produces(MediaType.TEXT_PLAIN)
	public String updateAnswer(@PathParam("answerID") String answerID, @PathParam("questionID") String questionID, 
			@PathParam("answerDesc") String answerDesc, @PathParam("answerValue") String answerValue,  
			@PathParam("solution") String solution)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	AnswerItem a = AnswerItem.getAnwserByID(Integer.valueOf(answerID), session);
			 	a.setAnsiDesc(answerDesc);
			 	a.setQuestion(Question.getQuestionByID(Integer.valueOf(questionID), session));
			 	a.setAnsiValue(answerValue);
			 	boolean isSolution = Boolean.valueOf(solution);
			 	if(isSolution == true)
			 	{
			 		updateAllSolution(session, Integer.valueOf(answerID), Integer.valueOf(questionID));
			 	}
			 	a.setAnsiIsSolution(isSolution);
				session.saveOrUpdate(a);
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 session.getTransaction().commit();
		 return "success";
	}
	
	private void updateAllSolution(Session session, Integer rightAnswerID, Integer questionID)
	{
		List<AnswerItem> list = AnswerItem.getAnswerByQuestion(questionID, session);
		for (AnswerItem a : list) {
			if(a.getAnsiId() != rightAnswerID)
			{
				a.setAnsiIsSolution(false);
				session.saveOrUpdate(a);
			}
		}
	}
	
	@GET
	@Path("/delete/{answerID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String deleteAnswer(@PathParam("answerID") String answerID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 session.delete(AnswerItem.getAnwserByID(Integer.valueOf(answerID), session));
			 session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
}
